<template>
  <div class="mainBody">
    <div class="wrapper">
      <form action="#">
        <h1>欢迎登录</h1>
        <div class="input-box">
          <i class="fas fa-envelope icon"></i>
          <input id="usernameInput" type="text" required v-model="username" />
          <label>账号</label>
        </div>
        <div class="input-box">
          <i class="fas fa-lock icon"></i>
          <input id="passwordInput" type="password" required v-model="password" />
          <label>密码</label>
        </div>

        <div class="row">
          <a @click="forgotPassword">Forgot password?</a>
        </div>

        <button @click="login" class="btn">点击登录</button>

        <div class="signup-link">

        </div>
      </form>
    </div>
  </div>

</template>

<script setup lang="ts">
import {onMounted, ref} from 'vue';
import {ElMessage} from "element-plus";
import { userLoginService,getUserService} from "@/api/user.js"
import {useUserStore} from "@/stores/user/user.js";
import router from "@/router/index.js";
const userStore=useUserStore()
const forgotPassword = () => {
  alert('Please email ssnigdhasiraz22@sirhenryfloyd.co.uk to request a password reset');
};

onMounted( () => {
  //查询当前用户信息，如果有信息直接进行登录
  try {
    getUser()
  }catch (error){
    console.log(error)
  }

});


const getUser=(async ()=>{
  const res=await getUserService()
  if (res!=undefined&&res.code===200){
    //存储用户信息
    userStore.setUser(res.data)
    //跳转到导航页
    if (res.data!==null){
      await router.push('/layout')
    }
  }
})

//登录事件
const username=ref('admin')
const password=ref('123456')
const loginData=ref({})
const loginToken=ref('')
const login=(async (event)=>{
  event.preventDefault()
  if (username.value!=''&&password.value!=''){
    loginData.value={
      username:username.value,
      password:password.value
    }
    const res=await userLoginService(loginData.value)
    console.log(res)
    if (res.code===200){
      //存储token和用户信息
      ElMessage.success(res.data)
      loginToken.value=res.message
      userStore.setToken(loginToken.value)
      await router.push('/layout')
    }else {
      ElMessage.warning(res.errMsg)
    }

  }else {
    ElMessage.warning("请填写完整")
  }
})
</script>

<style>
* {
  font-family: "Poppins", sans-serif;
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.mainBody {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: white;
  background-size: cover;
  background-position: center;
  background-image: url("src/assets/back.jpg"); /* 替换为您自己的图片路径 */
}

.wrapper {
  background-color: rgba(255, 255, 255, 0.2);
  position: relative;
  width: 400px;
  height: 450px;
  border: 2px solid rgba(255, 255, 255, 0.5);
  border-radius: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(15px);
}

.wrapper:hover {
  box-shadow: 0 0 40px rgba(255,255,255,0.5);

}

.wrapper h1 {
  font-size: 2em;
  color: #fff;
  text-align: center;
}

.wrapper .input-box {
  position: relative;
  width: 310px;
  margin: 30px 0;
  border-bottom: 2px solid #fff;
}

.wrapper .input-box input {
  width: 100%;
  height: 50px;
  background: transparent;
  outline: none;
  border: none;
  font-size: 1em;
  color: #fff;
  padding: 0 40px 0 5px;
}

.wrapper .input-box label {
  position: absolute;
  top: 50%;
  left: 5px;
  transform: translateY(-50%);
  font-size: 1em;
  color: #fff;
  pointer-events: none;
  transition: 0.5s;
}

.wrapper .input-box input:focus ~ label,
.wrapper .input-box input:valid ~ label {
  top: -5px;
}

.wrapper .input-box .icon {
  position: absolute;
  right: 8px;
  color: #fff;
  font-size: 1.2em;
  line-height: 57px;
}

.wrapper .row {
  margin: -15px 0 15px;
  font-size: 0.9em;
  color: #fff;
  display: flex;
  justify-content: space-between;
}

.wrapper .row label {
  display: flex;
  align-items: center;
  gap: 5px;
}

.wrapper .row a {
  color: #fff;
  text-decoration: none;
}

.wrapper .options a:hover {
  text-decoration: underline;
}

.wrapper .btn {
  width: 100%;
  height: 40px;
  background: #fff;
  outline: none;
  border: none;
  border-radius: 40px;
  cursor: pointer;
  font-size: 1em;
  font-weight: 500;
  color: #000;
  margin-top: 10px;
}

.btn:hover {
  background: #ffffea;
}

.wrapper .signup-link {
  font-size: 0.9em;
  color: #fff;
  text-align: center;
  margin: 25px 0 10px;
}

.wrapper .signup-link a {
  color: #fff;
  text-decoration: none;
  font-weight: 600;
}

.wrapper .signup-link a:hover {
  text-decoration: underline;
}

@media (max-width: 360px) {
  .wrapper {
    width: 100%;
    height: 100vh;
    border: none;
    border-radius: 0px;
  }

  .wrapper .input-box {
    width: 290px;
  }
  #img {
    z-index: -90;
  }
}


</style>
